using System.Collections.ObjectModel;
using System.Windows;
using FU.Capstones.IMS.Web.Common;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.DataContracts;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.EventArgs;
using FU.Capstones.IMS.Web.Silverlight.Models;
using Microsoft.Practices.Prism.Commands;
using Navigation.Silverlight;

namespace FU.Capstones.IMS.Web.Silverlight.ViewModels
{
    public class EmploymentTypeDetailViewModel : ViewModelBase
    {
        public EmploymentTypeDetailViewModel()
        {
            Model = new EmploymentTypeDetailModel();
            ((EmploymentTypeDetailModel)Model).GetSingleEmploymentTypeCompleted += DoGetSingleEmploymentTypeCompleted;
            ((EmploymentTypeDetailModel)Model).InsertEmploymentTypeCompleted += DoInsertEmploymentTypeCompleted;
            ((EmploymentTypeDetailModel)Model).UpdateEmploymentTypeCompleted += DoUpdateEmploymentTypeCompleted;

            IsFirstLoaded = false;
            ErrorMessageVisibility = Visibility.Collapsed;
            MessageVisibility = Visibility.Collapsed;

            CanEditOrAdd = CurrentUser.CanEditEmploymentType || CurrentUser.CanAddEmploymentType;
            CanAdd = CurrentUser.CanAddEmploymentType;
        }

        #region Do Completed Methods

        void DoUpdateEmploymentTypeCompleted(object sender, FeedbackCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Message = e.Error.Message;
                SetMessageVisibility(false);
            }
            else
            {
                Message = e.Result.Message;
                SetMessageVisibility(e.Result.IsSuccessful);

                if (e.Result.IsSuccessful)
                {
                    Backup();
                }
            }
            ServiceCallCounter -= WebConstants.MagicNumer;
        }

        void DoInsertEmploymentTypeCompleted(object sender, SingleFeedbackCompletedEventArgs<int> e)
        {
            if (e.Error != null)
            {
                Message = e.Error.Message;
                SetMessageVisibility(false);
            }
            else
            {
                Message = e.Result.Message;
                SetMessageVisibility(e.Result.IsSuccessful);
                if (e.Result.IsSuccessful)
                {
                    isCreateNew = false;
                    EmploymentType.EmploymentTypeID = e.Result.Data;
                    Backup();
                }
            }
            ServiceCallCounter -= WebConstants.MagicNumer;
        }

        void DoGetSingleEmploymentTypeCompleted(object sender, SingleFeedbackCompletedEventArgs<EmploymentType> e)
        {
            if (e.Error != null)
            {
                Message = e.Error.Message;
                SetMessageVisibility(false);
            }
            else
            {
                Message = e.Result.Message;
                SetMessageVisibility(e.Result.IsSuccessful);
                if (e.Result.IsSuccessful)
                {
                    EmploymentType = new EmploymentType()
                    {
                        BriefDescription = e.Result.Data.BriefDescription,
                        EmploymentTypeID = e.Result.Data.EmploymentTypeID,
                        EmploymentTypeName = e.Result.Data.EmploymentTypeName,
                        EmploymentTypeCode =  e.Result.Data.EmploymentTypeCode,
                        FullDescription = e.Result.Data.FullDescription
                    };
                    Backup();
                }
            }
            ServiceCallCounter -= WebConstants.MagicNumer;
        }

        #endregion

        #region Commands

        #region SaveCommand

        private DelegateCommand<object> saveCommand;
        public DelegateCommand<object> SaveCommand
        {
            get
            {
                if (saveCommand == null)
                    saveCommand = new DelegateCommand<object>(DoSaveCommand);
                return saveCommand;
            }
        }

        private void DoSaveCommand(object obj)
        {
            EmploymentType.ValidateAll();
            if (!EmploymentType.HasErrors)
            {
                if (isCreateNew)
                {
                    ServiceCallCounter += ((EmploymentTypeDetailModel)Model).InsertEmploymentTypeAsync(EmploymentType);
                }
                else
                {
                    ServiceCallCounter += ((EmploymentTypeDetailModel)Model).UpdateEmploymentTypeAsync(EmploymentType);
                }
            }
            else
            {
                Message = "Dữ liệu nhập chưa đúng.";
                SetMessageVisibility(false);
            }
        }

        #endregion

        #region CancelCommand

        private DelegateCommand<object> cancelCommand;
        public DelegateCommand<object> CancelCommand
        {
            get
            {
                if (cancelCommand == null)
                    cancelCommand = new DelegateCommand<object>(DoCancelCommand);
                return cancelCommand;
            }
        }

        private void DoCancelCommand(object obj)
        {
            NavigationManager.NavigateTo(WebConstants.NavigationPages.EmploymentTypeList);
        }

        #endregion

        #region ResetCommand

        private DelegateCommand<object> resetCommand;
        public DelegateCommand<object> ResetCommand
        {
            get
            {
                if (resetCommand == null)
                    resetCommand = new DelegateCommand<object>(DoResetCommand);
                return resetCommand;
            }
        }

        private void DoResetCommand(object obj)
        {
            if (isCreateNew)
                EmploymentType = new EmploymentType();
            else
            {
                EmploymentType = eTypeTemp;
                Backup();
            }
        }

        #endregion

        #endregion

        #region Binding Properties

        private EmploymentType eType;
        public EmploymentType EmploymentType
        {
            get { return eType; }
            set
            {
                if (!ReferenceEquals(eType, value))
                {
                    eType = value;
                    RaisePropertyChanged(() => EmploymentType);
                }
            }
        }

        private ObservableCollection<EmploymentType> eTypeCollection;
        public ObservableCollection<EmploymentType> EmploymentTypeCollection
        {
            get { return eTypeCollection; }
            set
            {
                if (!ReferenceEquals(eTypeCollection, value))
                {
                    eTypeCollection = value;
                    RaisePropertyChanged(() => EmploymentTypeCollection);
                }
            }
        }

        #endregion

        #region Fields

        private bool isCreateNew;
        private EmploymentType eTypeTemp;

        #endregion

        #region Implementation of INavigationAware

        public override void OnNavigate(object extraData)
        {
            int? eTypeID = (int?)extraData;
            isCreateNew = (eTypeID.HasValue == false || eTypeID.Value == 0);
            if (!isCreateNew)
            {
                ServiceCallCounter += ((EmploymentTypeDetailModel)Model).GetSingleEmploymentTypeAsync(eTypeID.Value);
            }
            else
            {
                EmploymentType = new EmploymentType();
            }
            Message = "Đang kết nối tới máy chủ...";
        }

        public override void OnLeave()
        {
            //throw new NotImplementedException();
        }

        #endregion

        private void Backup()
        {
            if (EmploymentType != null)
            {
                eTypeTemp = new EmploymentType()
                {
                    BriefDescription = eType.BriefDescription,
                    EmploymentTypeID = eType.EmploymentTypeID,
                    EmploymentTypeName = eType.EmploymentTypeName,
                    EmploymentTypeCode = eType.EmploymentTypeCode,
                    FullDescription = eType.FullDescription
                };
            }
        }
    }
}